"use strict";
import Edge from "./Edge";
import globalOptions from "../Options/global";
import {hideLabelScale, Status} from "../Util/constant";

/**
 * 直线
 */
class StraightEdge extends Edge {


    /**
     * 绘制连线
     * @param {Object} body body
     */
    draw(body) {
        const e = this;
        const ctx = body.edgeContainer.getContext("2d");
        ctx.save();
        ctx.lineCap = "round";
        // 绘制连线

        // 处理线条样式
        let x1, y1, x2, y2, x3, y3;
        let x0 = e.target.x;
        let y0 = e.target.y;
        let k = (e.target.y - e.source.y) / (e.target.x - e.source.x);
        let r = e.target.r;
        let c = 1 / (Math.sqrt(1 + k * k));
        let p1 = r * k * c;
        let q1 = r * c;
        let p = k * (10 + r) * c;
        let q = (10 + r) * c;
        if (e.target.x > e.source.x) {
            x1 = x0 - q1;
            y1 = y0 - p1;
            x2 = x0 - q - 6 * k * c;
            y2 = y0 - p + 6 * c;
            x3 = x0 - q + 6 * k * c;
            y3 = y0 - p - 6 * c;
        } else {
            x1 = x0 + q1;
            y1 = y0 + p1;
            x2 = x0 + q - 6 * k * c;
            y2 = y0 + p + 6 * c;
            x3 = x0 + q + 6 * k * c;
            y3 = y0 + p - 6 * c;
        }
        ctx.lineWidth = 3;
        if (e.status === Status.default) {
            ctx.strokeStyle = e.options.color.default.border;
        } else {
            ctx.strokeStyle = e.options.color.selection.border;
        }
        this.drawArrow(ctx, x1, y1, x2, y2, x3, y3, ctx.strokeStyle);
        this.drawEdge(ctx, [e.source.x, e.source.y], [e.target.x, e.target.y], true);
        ctx.stroke();
        ctx.restore();
        if (globalOptions.edgeShowLabel && e.globalOptions.showLabel && e.label !== undefined && e.label !== null && e.body.scale.k >= hideLabelScale) {
            this.drawLabel(ctx, e);
        }
    }

    /**
     * 绘制节点名称
     * @param {Object} ctx canvas 上下文
     * @param {Object} e 当前边对象
     */
    drawLabel(ctx, e) {
        ctx.save();
        ctx.textBaseline = "middle";
        ctx.textAlign = "center";
        const opt = e.options;
        ctx.fillStyle = opt.font.color.default;
        ctx.font = `${opt.font.size.default}px ${opt.font.face}`;
        ctx.fillText(e.label, (e.source.x + e.target.x) / 2, (e.source.y + e.target.y) / 2);
        ctx.restore();
    }

    /**
     * 直线：实现 or 虚线
     * @param {Object} ctx canvas上下文
     * @param {Array} start 开始点位
     * @param {Array} end 结束点位
     * @param {Boolean} isSolid 是否实线
     */
    drawEdge(ctx, start, end, isSolid = true) {
        ctx.beginPath();
        if (!isSolid) {
            ctx.setLineDash([3, 5]);
        }
        ctx.moveTo(start[0], start[1]);
        ctx.lineTo(end[0], end[1]);
    }

    /**
     *    绘制箭头
     * @param ctx
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @param x3
     * @param y3
     * @param color
     * @param type
     */
    drawArrow(ctx, x1, y1, x2, y2, x3, y3, color) {
        ctx.beginPath();
        ctx.moveTo(x1, y1);
        ctx.lineTo(x2, y2);
        ctx.lineTo(x3, y3);
        ctx.lineTo(x1, y1);
        ctx.fillStyle = color;
        ctx.fill();
    }

}

export default StraightEdge;
